//
//  KADetailHeaderView.m
//  KeepAccount
//
//  Created by Veer on 2017/12/19.
//  Copyright © 2017年 Veer. All rights reserved.
//

#import "KAFindHeaderView.h"


@interface KAFindHeaderView()

@property (nonatomic, weak) UILabel *yearLbl;
@property (nonatomic, weak) UILabel *monthLbl;
@property (nonatomic, weak) UILabel *incomeLbl;
@property (nonatomic, weak) UILabel *expenditureLbl;

@property (nonatomic, strong) NSMutableArray *moneyLblArray;

@end


@implementation KAFindHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.moneyLblArray = [NSMutableArray array];
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView
{
    UILabel *yearLbl = [[UILabel alloc] init];
    yearLbl.text = @"账单";
    yearLbl.font = VRFont(16);
    yearLbl.textColor = RGB(122, 107, 60);
    [self addSubview:yearLbl];
    [yearLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(30);
    }];
    
    UIView *monthView = [[UIView alloc] init];
    [self addSubview:monthView];
    [monthView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-16);
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(76);
        make.height.mas_equalTo(30);
    }];
    
    // 月
    UILabel *monthLbl = [[UILabel alloc] init];
    monthLbl.text = @"12";
    self.monthLbl = monthLbl;
    monthLbl.font = VRFont(24);
    monthLbl.textColor = RGB(51, 51, 51);
    [monthView addSubview:monthLbl];
    [monthLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(2);
        make.left.mas_equalTo(15);
    }];
    
    UILabel *monthTitleLbl = [[UILabel alloc] init];
    monthTitleLbl.text = @"月";
    monthTitleLbl.font = VRFont(14);
    monthTitleLbl.textColor = RGB(51, 51, 51);
    [monthView addSubview:monthTitleLbl];
    [monthTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-1);
        make.left.mas_equalTo(monthLbl.mas_right).offset(2);
    }];
    
    // 竖线
    UIView *vLineView = [[UIView alloc] init];
    vLineView.backgroundColor = RGB(51, 51, 51);
    [self addSubview:vLineView];
    [vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(monthView).offset(-2);
        make.left.mas_equalTo(monthView.mas_right);
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(monthView).offset(-4);
    }];

    // 支出 expenditure
    CGFloat moneyW = (SCREEN_WIDTH - 76 - 20) / 3;
    
    NSArray *titleArray = @[@"收入", @"支出", @"结余"];
    for (int i = 0; i < 3; i++) {
        
        UILabel *expenditureTitleLbl = [[UILabel alloc] init];
        expenditureTitleLbl.text = titleArray[i];
        expenditureTitleLbl.font = VRFont(12);
        expenditureTitleLbl.textColor = RGB(122, 107, 60);
        [self addSubview:expenditureTitleLbl];
        [expenditureTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(monthView.mas_top);
            make.left.mas_equalTo(monthView.mas_right).offset(20 + moneyW * i);
        }];
        
        UILabel *expenditureLbl = [[UILabel alloc] init];
        expenditureLbl.text = @"91.00";
        expenditureLbl.font = VRFont(17);
        expenditureLbl.textColor = RGB(51, 51, 51);
        NSMutableAttributedString *expenditureLblAttrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",expenditureLbl.text]];
        [expenditureLblAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(expenditureLbl.text.length - 3, 3)];
        expenditureLbl.attributedText = expenditureLblAttrStr;
        [self addSubview:expenditureLbl];
        [expenditureLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(monthView).offset(0);
            make.left.mas_equalTo(expenditureTitleLbl).offset(0);
        }];
        
        [self.moneyLblArray addObject:expenditureLbl];
    }
    
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    [monthView addGestureRecognizer:tapGesture];
}


#pragma mark - action
- (void)handleTapGesture:(UITapGestureRecognizer *)recongnizer
{
    if (self.choseMonthBlock) {
        self.choseMonthBlock();
    }
}

#pragma mark setter & getter
- (void)setMonth:(NSInteger)month
{
    _month = month;
    
    self.monthLbl.text = [NSString stringWithFormat:@"%ld", (long)month];
}

- (void)setIncome:(double)income
{
    _income = income;
    
    UILabel *label = self.moneyLblArray[0];
    
    NSMutableAttributedString *expenditureLblAttrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f", income]];
    [expenditureLblAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(expenditureLblAttrStr.length - 3, 3)];
    label.attributedText = expenditureLblAttrStr;
}

- (void)setExpenditure:(double)expenditure
{
    _expenditure = expenditure;
    
    UILabel *label = self.moneyLblArray[1];
    
    NSMutableAttributedString *expenditureLblAttrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f", expenditure]];
    [expenditureLblAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(expenditureLblAttrStr.length - 3, 3)];
    label.attributedText = expenditureLblAttrStr;
}


- (void)setBalance:(double)balance
{
    _balance = balance;
    
    UILabel *label = self.moneyLblArray[2];
    
    NSMutableAttributedString *expenditureLblAttrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%.2f", balance]];
    [expenditureLblAttrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(expenditureLblAttrStr.length - 3, 3)];
    label.attributedText = expenditureLblAttrStr;
}


@end
